package org.example.blogserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.blogserver.dao.ArticleDao;
import org.example.blogserver.dao.CommentDao;
import org.example.blogserver.entity.Comment;
import org.example.blogserver.handler.TokenManager;
import org.example.blogserver.service.CommentsService;
import org.example.blogserver.service.SysUserService;
import org.example.blogserver.vo.CommentVo;
import org.example.blogserver.vo.Result;
import org.example.blogserver.vo.UserVo;
import org.example.blogserver.vo.params.CommentParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private TokenManager tokenManager;

    @Override
    public Result addComment(String token,CommentParam commentParam) {
        // 验证 token 是否有效
        String account = tokenManager.getAccountFromToken(token);
        if (account == null) {
            return Result.fail(400, "登录过期，请重新登录！");
        }
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setAuthorAccount(account);
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parent == 0 ? 0 : parent);
        this.commentDao.addComment(comment);
        UpdateWrapper<ArticleDao> updateWrapper = Wrappers.update();
        updateWrapper.eq("id",comment.getArticleId());
        updateWrapper.setSql(true,"comment_counts=comment_counts+1");
        return Result.success("评论成功！",null);
    }

    @Override
    public Result deleteCommentById(Long id) {
        int result = commentDao.deleteCommentById(id);
        if (result > 0) {
            return Result.success("删除成功！",null);
        } else {
            return Result.fail(500, "删除失败！");
        }
    }

    @Override
    public Result findCommentsVoByArticleId(Long articleId) {
        List<Comment> commentList = commentDao.findCommentsByArticleId(articleId);
        List<CommentVo> commentListVo = convertToCommentVoList(commentList);
        return Result.success("获取成功！",commentListVo);
    }

    public  CommentVo convertToCommentVo(Comment comment) {
        CommentVo commentVo = new CommentVo();
        commentVo.setId(String.valueOf(comment.getId()));
        commentVo.setContent(comment.getContent());
        commentVo.setLevel(comment.getLevel());
        commentVo.setChildren(null);

        // 设置作者信息
        UserVo author = sysUserService.getUserVoByAccount(comment.getAuthorAccount());
        commentVo.setAuthor(author);

        // 设置回复对象信息（假设 Comment 中有 toUser 字段）
        if (comment.getParentId() != 0) {
            Comment parent_comment = commentDao.findCommentById(comment.getParentId());
            UserVo toUser = sysUserService.getUserVoByAccount(parent_comment.getAuthorAccount());
            commentVo.setToUser(toUser);
        }

        // 如果是父评论，设置子评论列表
        if (comment.getParentId() == 0) {
            List<Comment> children =commentDao.findCommentsByParentId(comment.getId());
            List<CommentVo> childrenVo = convertToCommentVoList(children);
            commentVo.setChildren(childrenVo);
        }
        return commentVo;
    }

    public  List<CommentVo> convertToCommentVoList(List<Comment> commentList) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVo commentVo = convertToCommentVo(comment);
            commentVoList.add(commentVo);
        }
        return commentVoList;
    }

}
